package com.example.mysnwork;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {
    public static DataOutputStream dataOutputStream = null;

    private static byte sendbuff[] = new byte[5];
    private Button send;
    private Button close;
    private Button open;
    private Button conn;

    private EditText et_ip;
    private EditText et_port;

    private TextView ster;
    private  String ip;//服务器IP地址
    private int port;//服务器端口
    public static Socket socket = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ster = (TextView) findViewById(R.id.ster);
        et_ip = (EditText) findViewById(R.id.et_ip);
        et_port = (EditText) findViewById(R.id.et_port);
        send = (Button) findViewById(R.id.btn_send);
        close = (Button) findViewById(R.id.btn_close);
        open = (Button) findViewById(R.id.btn_open);
        conn = (Button) findViewById(R.id.btn_conn);
        conn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ster.setText("已连接");
                    }
                });
                stThread();
            }
        });
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (socket != null && socket.isConnected()) {
                    try {
                        socket.close();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ster.setText("已断开！");
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {sendbuff[0] = (byte)0x55;
                            sendbuff[1] = (byte)0xAA;
                            sendbuff[2] = (byte)0x00;
                            sendbuff[3] = (byte)0x01;
                            sendbuff[4] = (byte)0x00;
                            dataOutputStream.write(sendbuff);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
        open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {sendbuff[0] = (byte)0x55;
                            sendbuff[1] = (byte)0xAA;
                            sendbuff[2] = (byte)0x00;
                            sendbuff[3] = (byte)0x01;
                            sendbuff[4] = (byte)0x01;
                            dataOutputStream.write(sendbuff);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }
    private void stThread()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ip=et_ip.getText().toString().trim();
                port=Integer.parseInt(et_port.getText().toString().trim());
                try {
                    socket = new Socket(ip,port);
                    dataOutputStream = new DataOutputStream(socket.getOutputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}